package org.jks.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PageController {
	@RequestMapping("/")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, Config.HOME_INDEX);
		return new ModelAndView("index", model);
	}
	
	@RequestMapping("/index")
	public ModelAndView index1(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, Config.HOME_INDEX);
		return new ModelAndView("index", model);
	}
	
	@RequestMapping("/about")
	public ModelAndView about(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, Config.ABOUT_US_INDEX);
		return new ModelAndView("about", model);
	}
	
	@RequestMapping("/search")
	public ModelAndView search(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, Config.SEARCH_INDEX);
		return new ModelAndView("search", model);
	}
	
	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, 0);
		return new ModelAndView("register", model);
	} 
	
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, 0);
		return new ModelAndView("login", model);
	}
	
	@RequestMapping("/logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		if(session.getAttribute("username") != null){
			session.removeAttribute("username");
		}
		Config.init(request, response, session, model, 0);
		return new ModelAndView("login", model);
	}
	
	@RequestMapping("/error")
	public ModelAndView error(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model, 0);
		return new ModelAndView("error", model);
	}
}
